package com.bbs.bo;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.bbs.util.HbnUtils;
import com.bbs.vo.Bbs;

/**
 * 对数据库中帖子表的操作类
 * 
 * @author Wangpeihu
 * @since 05/01/2010
 * @Team [Never give up]
 */
public class BbsDao {

	/**
	 * 增加一个帖子
	 * 
	 * @param bbs
	 */
	public void addBbs(Bbs bbs) {
		Session s = HbnUtils.getSession();

		try {
			s.beginTransaction();
			s.save(bbs);
			s.getTransaction().commit();
		} catch (HibernateException e) {
			s.getTransaction().rollback();
			throw new RuntimeException(e);
		} finally {
			s.close();
		}
	}

	/**
	 * 删除一个或多个帖子
	 * 
	 * @param emp
	 */
	public void deleteBbs(Integer[] bbsId) {
		Session s = HbnUtils.getSession();
		try {
			s.beginTransaction();
			for (int i = 0; i < bbsId.length; i++) {
				s.delete(s.get(Bbs.class, bbsId[i]));
			}
			s.getTransaction().commit();
		} catch (HibernateException e) {
			s.getTransaction().rollback();
			throw new RuntimeException(e);
		} finally {
			s.close();
		}
	}

	/**
	 * 更新一个帖子
	 * 
	 * @param bbs
	 */
	public void updateBbs(Bbs bbs) {
		Session s = HbnUtils.getSession();
		try {
			s.beginTransaction();
			s.update(bbs);
			s.getTransaction().commit();
		} catch (HibernateException e) {
			s.getTransaction().rollback();
			throw new RuntimeException(e);
		} finally {
			s.close();
		}
	}

	/**
	 * 获得全部的帖子信息
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Bbs> findAllBbs() {
		Session s = HbnUtils.getSession();
		Query query = s.createQuery("from Bbs");
		List<Bbs> empList = query.list();
		s.close();
		return empList;
	}

}